package com.fuyun.microservice.config;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.Config;
import org.redisson.config.SentinelServersConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import static com.fuyun.microservice.GlobalConstant.DEFAULT_DATE_TIME_FORMAT;

/**
 * RedissonConfig配置
 *
 * @author zqh
 * @date 2019/3/30 0030
 */
@Configuration
@EnableConfigurationProperties(RedisProperties.class)
public class RedissonConfiguration {

//    @Value("${spring.redis.address}")
//    String redisAddress;
//
//    @Value("${spring.redis.password:}")
//    String redisPassword;
//
//    @Value("${spring.redis.password:}")
//    int redisDatabase=0;

    @Autowired
    RedisProperties redisProperties;

    @Bean(value = "sessionRedission" ,destroyMethod = "shutdown")
    public RedissonClient redissonClient() {
        Config config = new Config();
        SentinelServersConfig sentinelServersConfig = config.useSentinelServers()
                .addSentinelAddress(redisProperties.getSentinel().getNodes().split(","))
                .setMasterName(redisProperties.getSentinel().getMaster())
                .setTimeout(redisProperties.getTimeout());

        if (StringUtils.isNotBlank(redisProperties.getPassword())) {
            sentinelServersConfig.setPassword(redisProperties.getPassword());
        }

        config.setCodec(new JsonJacksonCodec(objectMapper()));
        return Redisson.create(config);
    }


    public ObjectMapper objectMapper(){
        ObjectMapper mapper = new ObjectMapper();
        //大小写不敏感
        mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES,true);
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.setDateFormat(new SimpleDateFormat(DEFAULT_DATE_TIME_FORMAT));
        JavaTimeModule javaTimeModule = new JavaTimeModule();
        javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)));
        javaTimeModule.addDeserializer(LocalDateTime.class,new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)));
        mapper.registerModule(javaTimeModule);
        return mapper;
    }
}

